﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using SocketClient;
using System.Net.Sockets;
using SkypengerApp.Data;
using System.ComponentModel;

namespace SkypengerApp.UserControls
{

    /// <summary>
    /// Logique d'interaction pour Client.xaml
    /// </summary>
    public partial class Client : UserControl
    {
        public Contact MyContact { get; set; }
        public string MyPseudo { get; set; }
        public bool IsOnline { get; set; }
        private bool IsCalled = false;

        public Client(string name, string ipAddress, int port, string myPseudo, bool isOnline)
        {
            InitializeComponent();

            this.Lbl_Name.Content = string.IsNullOrEmpty(name) ? ipAddress.ToString() : name;
            MyContact = new Contact();
            MyContact.Name = name;
            MyContact.IP = ipAddress;
            MyContact.Port = port;
            MyPseudo = myPseudo;
            IsOnline = isOnline;

            this.Loaded += new RoutedEventHandler(Client_Loaded);
        }

        void Client_Loaded(object sender, RoutedEventArgs e)
        {
            this.MainBorder.MouseLeftButtonDown += new MouseButtonEventHandler(MainBorder_MouseLeftButtonDown);

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            bool connected = false;

            if (!IsOnline)
            {
                connected = SocketClientHelper.RequestIsOnline(MyContact.IP, MyContact.Port);
            }

            this.Dispatcher.Invoke((Action)delegate() { this.SetState(connected); });
            
        }

        void MainBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (IsOnline && !IsCalled)
            {
                TcpClient socket = SocketClientHelper.Connect(MyContact.IP, MyContact.Port);
                Conversation window = new Conversation(MyPseudo, MyContact.Name, MyContact.IP, socket);
                window.Closed += (sdr, evt) =>
                    {
                        IsCalled = false;
                    };
                window.Show();
                IsCalled = true;
            }
        }

        public void SetState(bool connected)
        {
            if (connected)
            {
                IsOnline = true;
                this.Lbl_Connect.Content = "En ligne";
                this.MainBorder.BorderBrush = new SolidColorBrush(Colors.Green);
            }
            else
            {
                IsOnline = false;
                this.Lbl_Connect.Content = "Hors-ligne";
                this.MainBorder.BorderBrush = new SolidColorBrush(Colors.Gainsboro);
            }
        }
    }
}
